
<template>
    <div class="agri-trace">
      <header class="header">
        <h1>农产品溯源系统</h1>
      </header>
      
      <main class="main">
        <div class="search-section">
          <input 
            v-model="traceCode" 
            @keyup.enter="searchProduct"
            type="text" 
            placeholder="输入产品溯源码"
            class="search-input"
          >
          <button @click="searchProduct" class="search-button">查询</button>
        </div>
  
        <div v-if="product" class="product-info">
          <h2>{{ product.traceName }}</h2>
          <p><strong>产地：</strong> {{ product.traceOrigin }}</p>
          <p><strong>生产日期：</strong> {{ product.startDate }}</p>
          <p><strong>保质期：</strong> {{ product.lastDate }}</p>
        </div>
  
        <div v-if="product" class="timeline">
          <h3>产品旅程</h3>
          <ul class="timeline-list">
            <li v-for="(event, index) in product.timeLine" :key="index" class="timeline-item">
              <div class="timeline-date">{{ event.eventDate }}</div>
              <div class="timeline-content">
                <h4>{{ event.eventTitle }}</h4>
                <p>{{ event.eventDescription }}</p>
              </div>
            </li>
          </ul>
        </div>
      </main>
  
      <footer class="footer">
        <p>&copy; 2025 优质农产品溯源系统 | 技术支持：吱吱农场</p>
      </footer>
    </div>
  </template>

<script setup>
import TopBar from '@/component/TopBar.vue'
import Footer from '@/component/Footer.vue'
import { ref, onMounted } from 'vue'
import request from '../../utils/request';
import { useRoute } from "vue-router";

const traceCode = ref('')
const product = ref(null)

const searchProduct = () => {
  request.get(`/api/qr/GetTraceDesc`,{
    params:{
      traceUid:traceCode.value
    }
  }).then(res=>{
    product.value=res.data
  })
}

const GetTrace=()=>{
  const route=useRoute();
  const traceUid=route.params.qrCode;
  request.get(`/api/qr/GetTraceDesc`,{
    params:{
      traceUid:traceUid
    }
  }).then(res=>{
    product.value=res.data
  })
}

onMounted(async ()=>{
  GetTrace();
  console.log(product.value);
})

</script>

<style>
:root {
  --primary-color: #4CAF50;
  --secondary-color: #45a049;
  --text-color: #333;
  --background-color: #f4f4f4;
  --border-color: #ddd;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  color: var(--text-color);
  background-color: var(--background-color);
}

.agri-trace {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  background-color: var(--primary-color);
  color: white;
  text-align: center;
  padding: 1rem;
  margin-bottom: 2rem;
}

.search-section {
  display: flex;
  margin-bottom: 2rem;
}

.search-input {
  flex-grow: 1;
  padding: 10px;
  font-size: 16px;
  border: 1px solid var(--border-color);
  border-right: none;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.search-button {
  padding: 10px 20px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  cursor: pointer;
  font-size: 16px;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  transition: background-color 0.3s;
}

.search-button:hover {
  background-color: var(--secondary-color);
}

.product-info {
  background-color: white;
  border: 1px solid var(--border-color);
  padding: 20px;
  margin-bottom: 2rem;
  border-radius: 4px;
}

.product-info h2 {
  color: var(--primary-color);
  margin-bottom: 1rem;
}

.timeline {
  background-color: white;
  border: 1px solid var(--border-color);
  padding: 20px;
  border-radius: 4px;
}

.timeline h3 {
  color: var(--primary-color);
  margin-bottom: 1rem;
}

.timeline-list {
  list-style-type: none;
}

.timeline-item {
  border-left: 2px solid var(--primary-color);
  padding-left: 20px;
  padding-bottom: 20px;
  position: relative;
}

.timeline-item:last-child {
  padding-bottom: 0;
}

.timeline-item::before {
  content: '';
  width: 12px;
  height: 12px;
  background-color: var(--primary-color);
  border-radius: 50%;
  position: absolute;
  left: -7px;
  top: 0;
}

.timeline-date {
  font-weight: bold;
  margin-bottom: 5px;
}

.timeline-content h4 {
  color: var(--primary-color);
  margin-bottom: 5px;
}

.footer {
  margin-top: 2rem;
  text-align: center;
  color: #666;
  font-size: 0.9rem;
}

@media (max-width: 600px) {
  .agri-trace {
    padding: 10px;
  }

  .search-section {
    flex-direction: column;
  }

  .search-input {
    border-right: 1px solid var(--border-color);
    border-bottom: none;
    border-radius: 4px 4px 0 0;
  }

  .search-button {
    border-radius: 0 0 4px 4px;
  }
}
</style>